package org.footito.android.tools;

import java.io.File;
import java.io.IOException;

import org.footito.android.data.Team;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;

public class Tools {
	public static void share(Context context, String message) {
		Intent sharingIntent = new Intent(Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
		sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Footito");
		context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
	}

	public static void checkFilesFolder() {
		File f = Environment.getExternalStorageDirectory();
		// methode pour tester l'existence
		if (f.canWrite()) {
			f = new File(Team.PICTURE_FOLDER);
			Log.d("Can write: " + "name:" + f.getName() + " path:" + f.getPath());
			if (!f.exists()) {
				Log.d("Mkdir: " + f.mkdirs());
				if (f.canWrite()) {
					Log.d("Infos: " + "name:" + f.getName() + " path:" + f.getPath());
					f = new File(Team.PICTURE_FOLDER + ".nomedia");
					try {
						f.createNewFile();
					} catch (IOException e) { 
					}
				} else {
					Log.e("Can not write: " + f.getName());
				}
			} else {
				Log.e("Exist: " + f.getName());
				f = new File(Team.PICTURE_FOLDER + ".nomedia");
				if (!f.exists())
					try {
						f.createNewFile();
					} catch (IOException e) {
					}
				else
					Log.d("Exist: " + f.getName());
			}

		} else {
			Log.e("Can not write : " + "scdard");
		}
	}

	public static boolean hasSDCard() {
		String state = Environment.getExternalStorageState();
		Log.w(state);
		boolean requireWriteAccess = true;
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			return true;
		} else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			return true;
		}
		return false;
	}
}
